Skip to content

PROF-16674 Make ElfParser follow the gABI for symbols, the dynamic array and PLT relocations - #1024

Open
zhengyu123 wants to merge 15 commits into
mainfrom
zgu/PROF-16674-elf-dynamic
Open

zhengyu123 wants to merge 15 commits into
mainfrom
zgu/PROF-16674-elf-dynamic

Conversation

@zhengyu123

@zhengyu123 zhengyu123 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?:
Makes ElfParser (symbols_linux.cpp) follow the gABI in four places:

  • Symbol tables: loadSymbolTable skips SHN_UNDEF, reserved section indices (except SHN_XINDEX), STT_TLS, STT_SECTION and STT_FILE symbols. They no longer become code-cache entries.
  • Dynamic array: parseDynamicSection stops at the first DT_NULL and reads whole Elf64_Dyn entries only.
  • PLT relocations: the DT_JMPREL entry size comes from DT_PLTREL, not from DT_RELENT/DT_RELAENT. DT_RELA and DT_REL now have separate size, entry-size and count variables.
  • Relocation entry sizes: a DT_RELAENT smaller than sizeof(Elf64_Rela) skips the .rela.dyn table. Without DT_PLTREL, the same value skips the PLT table instead of falling back to DT_RELENT.

hashImportLayout (the image fingerprint) uses the same tables and entry sizes, so the fingerprint covers what the parser records. The relative-relocation skip (DT_RELACOUNT × entry size) is clamped without overflow.

Motivation:
Findings 1-4 of PROF-16674. Bogus code-cache entries, imports lost for libraries with only PLT relocations, and stale slots after DT_NULL overriding real tags. Checked against the TIS ELF 1.2 spec for the dynamic tags, special section indices and symbol types.

Additional Notes:

  • Behavior change, please acknowledge: the early return on relent == 0 is gone. Images with no relocation entry-size tags (the vDSO, relocation-free DSOs) now load their .dynsym into the code cache. This is the fix proposed in PROF-16641 and is tested.
  • PROF-16641: implemented here except exact-size validation. A DT_RELAENT above 24 is still accepted.
  • PROF-16642: only the DT_NULL stop is done. The parser still reads p_memsz and the fingerprint p_filesz.
  • Not in this PR: a NULL PT_DYNAMIC dereference and a zero fingerprint when DT_STRSZ is missing (PROF-16755). Both were already on main.
  • Known remaining, found in review: without DT_PLTREL and DT_RELAENT, the PLT is read at the DT_RELENT stride. A DT_PLTREL value other than REL/RELA falls back silently. The parser and the fingerprint scan the tags separately.
  • Human review: recommended. The change decides which symbols and imports are recorded from untrusted in-memory images, and the fingerprint LibraryPatcher uses to tell a reloaded image apart.
  • Review done: Sphinx (standard) over the branch, then a medium /code-review on each fix range. No Critical or High findings.

How to test the change?:
./.claude/commands/build-and-summarize :ddprof-lib:gtestDebug_elfparser_ut (Linux only; the file is #ifdef __linux__).

  • The new tests build hand-made ELF images and cover each branch: non-address symbols (including the SHN_LORESERVE boundary and SHN_XINDEX), PLT-only imports, DT_PLTREL over DT_RELAENT, the DT_REL/DT_RELA PLT entry strides and the DT_RELAENT/DT_RELENT fallback strides (parse and fingerprint), the DT_NULL stop, .dynsym without relocation tags, and relative-count overflow for DT_RELA and DT_REL.
  • Reverting each fix made its test fail at eaee154de: the relent early return, the unclamped count, the DT_RELAENT size check and the PLT fallback. The stride tests added in review are not mutation-checked.
  • At eaee154de: elfparser_ut passed 43 of 43 on glibc, and the full :ddprof-lib:gtestDebug passed on glibc and musl. At bf600b474: the elfparser_ut task passed on glibc. All runs were in podman containers on aarch64 only; x86-64 has not been run locally.

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles
    credentials of any kind, I've requested a security review (run the dd:platform-security-review
    skill, or file a request via the PSEC review form).
    bewaire also runs automatically on every PR.
  • This PR doesn't touch any of that.
  • JIRA: PROF-16674

Unsure? Have a question? Request a review!

🤖 Generated with Claude Code

zhengyu123 and others added 9 commits November 17, 2026 16:01
…bles

loadSymbolTable added every symbol with a non-zero st_value at
base + st_value. Per the gABI that is wrong for three kinds of symbol:

- STT_TLS: st_value is an offset into the TLS template, so the entry
  landed at the image start (libjvm, glibc).
- SHN_ABS and other reserved section indices: st_value is not an address,
  but it was relocated by the load bias anyway.
- SHN_UNDEF: in an executable a non-zero st_value is the PLT stub address,
  so the stub was named after the real function.

Skip these, and the STT_SECTION and STT_FILE bookkeeping entries. Add a
test with a hand-built ELF image. Give the symbols in
symbolTableMisalignedEntries a defined section index, as real symbols have.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…m DT_PLTREL

parseDynamicSection had three gABI defects:

- It walked p_memsz bytes and ignored DT_NULL, so stale slots after the
  terminator (patchelf, prelink) could override real tags. A p_memsz that
  is not a multiple of sizeof(ElfDyn) also read a partial entry.
- It returned early when DT_RELENT/DT_RELAENT was missing. A library with
  only PLT relocations has neither, so it lost all its imports and the
  hooks were not patched. The DT_JMPREL entry size now comes from
  DT_PLTREL.
- DT_REL and DT_RELA shared one set of variables, so one table hid the
  other. They now have separate tables.

Apply the same entry sizes and tables in hashImportLayout, so the image
fingerprint covers what the parser records. Add tests with hand-built
in-memory images for a PLT-only library and for tags after DT_NULL.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ile and common symbols

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…thout overflow, test DT_PLTREL precedence

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…print, assert the strAt() bound test, drop dead clamps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e size

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… strAt() bound with a control; guard skip <= size

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…4_Rela

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cannot pick DT_RELENT for a RELA table

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zhengyu123
zhengyu123 requested a review from a team as a code owner November 19, 2026 13:37
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T20:45:53.412782Z bf600b4 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Import parsing and fingerprinting use consistent relocation strides for PLT-only images without DT_RELENT/DT_RELAENT. Static review found no actionable defects; runtime behavior was not exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit eaee154

@dd-octo-sts

dd-octo-sts Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

CI Test Results

Run: #44814626519 | Commit: 0f2758c | Duration: 20m 36s (longest job)

✅ All 32 test jobs passed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
8 - ✅ - -
8-ibm - ✅ - -
8-j9 ✅ ✅ - -
8-librca - - ✅ ✅
8-orcl - ✅ - -
11 - ✅ - -
11-j9 ✅ ✅ - -
11-librca - - ✅ ✅
17 ✅ ✅ - -
17-graal ✅ ✅ - -
17-j9 ✅ ✅ - -
17-librca - - ✅ ✅
21 ✅ ✅ - -
21-graal ✅ ✅ - -
21-librca - - ✅ ✅
25 ✅ ✅ - -
25-graal ✅ ✅ - -
25-librca - - ✅ ✅

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Summary: Total: 32 | Passed: 32 | Failed: 0


Updated: 2026-11-20 21:10:58 UTC

@dd-octo-sts

dd-octo-sts Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ All 40 integration tests passed

📊 Dashboard · 👷 Pipeline · 📦 405e6b48

@dd-octo-sts

dd-octo-sts Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Reliability & Chaos Results

✅ All reliability & chaos checks passed Pipeline: https://gitlab.ddbuild.io/DataDog/java-profiler/-/pipelines/154758098

@zhengyu123

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Static review found no actionable defect in symbol filtering, dynamic-array termination, or relocation parsing and fingerprinting. Runtime behavior remains unverified because builds and tests were not run.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit eaee154

kaahos
kaahos previously approved these changes Nov 20, 2026

@kaahos kaahos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I left 2 comments about branches that are not tested I think but the implementation is good to me.

jbachorik and others added 4 commits November 20, 2026 19:05
…of pltEntrySize

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…, cover valid DT_RELAENT and DT_PLTREL=DT_RELA

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…cover each REL/RELA and fallback stride positively

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
zhengyu123 and others added 2 commits November 20, 2026 15:39
…, drop a duplicate row, add RELENT/RELAENT rows

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…second entry inside the table

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@zhengyu123 zhengyu123 added the human_review:recommended Human review recommended — see PR description label Nov 20, 2026
@zhengyu123
zhengyu123 requested a review from kaahos November 20, 2026 21:15

@jbachorik jbachorik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sphinx Review

Automated review findings for this pull request are posted as inline comments below.

Reviewed commit: 405e6b4

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

static size_t pltEntrySize(uint64_t pltrel, size_t relent, size_t relaent) {
if (pltrel == DT_REL) return sizeof(Elf64_Rel);
if (pltrel == DT_RELA) return sizeof(Elf64_Rela);
return relaent != 0 ? relaEntrySize(relaent) : relent;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 pltEntrySize() silently falls back to relaent/relent when DT_PLTREL is present but holds any value other than DT_REL/DT_RELA (e.g. a corrupted or nonstandard image).

A bogus DT_PLTREL is exactly the case where the stride is unknown, yet the code guesses one and walks the untrusted DT_JMPREL table at that guessed stride anyway, both in parseDynamicSection() (wrong imports can be recorded: a misaligned r_offset + a coincidentally resolvable symbol name registers a bogus addImport slot for LibraryPatcher) and in hashImportLayout() (fingerprint of garbage). The PR notes acknowledge this ("A DT_PLTREL value other than REL/RELA falls back silently"), but for a table whose entry type is explicitly declared and wrong, the safe gABI behavior is to skip the table, as relaEntrySize() already does for undersized DT_RELAENT.

  • (a) Treat an out-of-range DT_PLTREL as invalid: if (pltrel != 0 && pltrel != DT_REL && pltrel != DT_RELA) return 0; before the fallback, so the table is skipped the same way relaEntrySize() skips undersized entries.
  • (b) Alternatively, keep the fallback but log a warning once per image, so a nonstandard DT_PLTREL is diagnosable instead of silently mis-strided.
  • Add a test mirroring pltRelOverridesRelaent with DT_PLTREL = 0x1234 asserting the PLT table is skipped in both the parse and the fingerprint.
Evidence
static size_t pltEntrySize(uint64_t pltrel, size_t relent, size_t relaent) {
    if (pltrel == DT_REL) return sizeof(Elf64_Rel);
    if (pltrel == DT_RELA) return sizeof(Elf64_Rela);
    return relaent != 0 ? relaEntrySize(relaent) : relent;
}

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

// Imports can also be in .rela.dyn / .rel.dyn, for example if a shared
// library is built without PLT (-fno-plt). If both tables exist,
// addImport saves them both.
addImports(rela, relasz, relaEntrySize(relaent), relativeBytes(relacount, relaent, relasz), true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4 For the .rela.dyn table, the stride is taken from DT_RELAENT and the table is skipped entirely when the tag is absent (relaEntrySize(0) returns 0).

Per the gABI the Elf64_Rela entry size for a DT_RELA table is implied by the format, and DT_RELAENT is not required to be present; a spec-valid image with DT_RELA/DT_RELASZ but no DT_RELAENT silently loses all its R_GLOB_DAT/R_ABS64 imports (e.g. pthread_create for a -fno-plt library), which LibraryPatcher then cannot patch. The same applies to .rel.dyn with a missing DT_RELENT. This is not a regression (the old code required a non-zero relent too), and every mainstream linker emits the *ENT tags, so it only bites hand-built or stripped-tag images — but the PR's own rationale for the DT_JMPREL fix was exactly that a table must not depend on an entry-size tag being present.

  • Default relaent to sizeof(Elf64_Rela) (and relent to sizeof(Elf64_Rel)) when the tag is absent, using the tag value only when present and >= the implied size — mirroring how pltEntrySize() now derives the DT_JMPREL stride without relying on the *ENT tags.
  • Or document explicitly (comment + PR notes) that DT_RELAENT/DT_RELENT absence is treated as invalid by design, so the asymmetry with DT_JMPREL is intentional.
Evidence
addImports(rela, relasz, relaEntrySize(relaent), relativeBytes(relacount, relaent, relasz), true);
        addImports(rel, relsz, relent, relativeBytes(relcount, relent, relsz), true);

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

// Not SHN_UNDEF (defined elsewhere), reserved indices other than SHN_XINDEX (absolute
// value or alignment), STT_TLS (TLS offset) or STT_SECTION/STT_FILE.
static bool isDefinedInImageSymbol(const ElfSymbol& entry) {
if (entry.st_shndx == SHN_UNDEF || (entry.st_shndx >= SHN_LORESERVE && entry.st_shndx != SHN_XINDEX)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 SHN_XINDEX is not itself evidence that a symbol is defined in the image: it signals that the actual section index is stored in the associated SHT_SYMTAB_SHNDX table.

This filter accepts every such symbol without resolving that index, including entries whose actual index is SHN_UNDEF or another special index, and can add a non-address value to the code cache.

Resolve SHN_XINDEX through the section's associated SHT_SYMTAB_SHNDX data before deciding whether the symbol is an in-image definition; if that table is unavailable or invalid, skip the symbol.

Evidence
if (entry.st_shndx == SHN_UNDEF || (entry.st_shndx >= SHN_LORESERVE && entry.st_shndx != SHN_XINDEX)) {

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

static size_t pltEntrySize(uint64_t pltrel, size_t relent, size_t relaent) {
if (pltrel == DT_REL) return sizeof(Elf64_Rel);
if (pltrel == DT_RELA) return sizeof(Elf64_Rela);
return relaent != 0 ? relaEntrySize(relaent) : relent;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4 No test in this PR would detect: using relent instead of a valid relaent for the fallback stride in pltEntrySize() when DT_PLTREL is absent in pltEntrySize

Add a test with DT_PLTREL absent and a valid DT_RELAENT that differs from DT_RELENT, then assert the PLT import is parsed and its relocation table affects the fingerprint.

Evidence
return relaent != 0 ? relaEntrySize(relaent) : relent;

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🧞 Sphinx — automated review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human_review:recommended Human review recommended — see PR description

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants